<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Order_goods;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class  OrdersController extends Controller
{	
	// 下单操作 创建订单
	public function insert(Request $request){
		// dd($request->all());
		// dd(session('id'));
		$address = AddressController::getAllAddressById(session('id'));
		//显示订单信息 完善订单信息
		// dd($address);
		// 总计
		$total =0;
		// 小计
		$xiaoji=0;
		$goods = session('car');
		// dd($goods);
		$data = [];
		if(!empty($goods)){
		foreach ($goods as $k => $v) {
		
		$tmp = DB::table('goods')->where('id',$v['id'])->first();
		$tmp->num = $v['num'];
		$xiaoji = $tmp->price * $tmp->num;
		$tmp->xiaoji =$xiaoji;
		$total+=$xiaoji;
		$data[] = $tmp;
		}
	// dd($data);
		return view('order.address',['address'=>$address,'goods'=>$data,'total'=>$total]);
	}else{
		return view('goods.emptycar');
	}
	}

	//创建订单
	public function create(Request $request){
		 // dd($request->all());

		//创建模型 order
		$order = new \App\models\Order;
		$order->user_id = session('id');
		$order->order_code = $this->getOrderCode();
		$order->address_id = $request->input('address_id');
		$order->total = $this->getTotal();
		// dd($this->getTotal());
		$order->status = 1;
		//进行数据添加
		$res = $order->save();
		// dd($res);
		if($res){
			//开始对订单详情表 order_goods
			$goods = session('car');
			$data = [];
			foreach ($goods as $k => $v) {
				$tmp['order_id'] = $order->id;//获取上面插入的id
				$tmp['goods_id'] = $v['id'];
				$tmp['num'] = $v['num'];
				$data[] = $tmp;
			}
			$oid = $tmp['order_id'];
			// dd($oid);
			//执行数据插入
			$id = order_goods::insert($data);
			if($id){
				// return redirect("order/finish");
				return redirect()->action('OrdersController@finish',['oid'=>$oid]);
			}else{
    		//失败 回到添加页
    		return back()->with('error','订单提交失败');
		}
	}
}
	//获取订单编号
	public function getOrderCode(){
		return time().rand(100000,999999);
	}

	//获取订单总价
	public function getTotal(){
		//获取购物车商品
		$goods = session('car');
		// dd($goods);
		$t = 0;
		//遍历
		foreach ($goods as $key => $value) {
			//获取商品单价
			$price = DB::table('goods')->where('id',$value['id'])->first()->price;
			// dd($price);
			$t += $value['num']*$price;

		}
		// dd($t);
		return $t;
	}

	//订单提交成功页面
	public function finish(Request $request){
		//清空购物车
		$request->session()->forget('car');


		// dd($request ->all());
		$id = $request->input('oid');
		// dd($id);

		$res = DB::table('orders') -> where('id',$id)->first();
		// dd($res);
		$data['id'] = $res->id;
		$data['order_code'] = $res->order_code;
		$data['address_id'] = $res->address_id;
		$data['order_id'] = $res->id;
		$data['total'] = $res->total;
		// dd($data);
		$der = DB::table('addresses') -> where('id',$data['address_id'])->first();
		// dd($der);
		$data['sheng'] = $der->sheng;
		$data['shi'] = $der->shi;
		$data['xian'] = $der->xian;
		$data['jiedao'] = $der->jiedao;
		$data['phone'] = $der->phone;
		$data['getman'] = $der->getman;
		// dd($data);
		$der2 = DB::table('order_goods') -> where('order_id',$data['order_id'])->first();
		$data['goos_id'] = $der2->goods_id;
		$der4 = DB::table('goods') -> where('id',$data['goos_id'])->first();
		$data['proname'] = $der4->proname;
		// dd($data);

		//显示页面
		return view('order.finish',['data'=>$data]);
	}
	public function jiesuan(Request $request)
	{
		// dd($request ->all());
		$id = $request->input('id');
		// dd($id);
		$res = DB::table('orders') -> where('id',$id)->first();
		$data['id'] = $res->id;
		$data['order_code'] = $res->order_code;
		// $data['address_id'] = $res->address_id;
		// $data['order_id'] = $res->id;
		$data['total'] = $res->total;
		$data['user_id'] = $res->user_id;
		$der = DB::table('users') ->where('id',$data['user_id'])->first();
		// dd($der);
		$dat['money'] = $der->money;
		$da = $dat['money']-$data['total'];
		$der1 = DB::table('users') ->where('id',1)->first();
		// dd($der1);
		$der2['money'] = $der1->money;
		$der3 = $der2['money']+$data['total'];
		// dd($der3);
		// dd($da);
		$res1 = DB::table('users')->where('id',$data['user_id'])->update(['money'=>$da]);
		if($res1){
			$res2 = DB::table('users')->where('id',1)->update(['money'=>$der3]);
			if($res2){
				return view('/evaluate/succ',['id'=>$id]);
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
}
	

